package com.yuyang.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyang.common.constant.ProductConstants;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.yuyang.gulimall.product.dao.AttrGroupDao;
import com.yuyang.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.yuyang.gulimall.product.entity.AttrEntity;
import com.yuyang.gulimall.product.entity.AttrGroupEntity;
import com.yuyang.gulimall.product.service.AttrAttrgroupRelationService;
import com.yuyang.gulimall.product.service.AttrGroupService;
import com.yuyang.gulimall.product.service.AttrService;
import com.yuyang.gulimall.product.vo.AttrGroupRelationVo;
import com.yuyang.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        lambdaQueryWrapper.eq(catelogId != 0, AttrGroupEntity::getCatelogId, catelogId).and(StringUtils.isNotBlank(key), (wrapper) -> {
            wrapper.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getAttrGroupName, key);
        });

        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), lambdaQueryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId));
        List<Long> attrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if (attrIds.size() == 0) {
            return null;
        } else {
            return attrService.listByIds(attrIds);
        }
    }

    @Override
    public void deleteAttrRelation(AttrGroupRelationVo[] attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> entityList = Arrays.stream(attrGroupRelationVos).map((vos) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vos, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatchAttrRelation(entityList);
    }

    @Override
    public PageUtils getNoAttrRelated(Map<String, Object> params, Long attrGroupId) {
        //查询当前属性分组的信息
        AttrGroupEntity attrGroupEntity = this.getOne(new LambdaUpdateWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getAttrGroupId, attrGroupId));
        Long catelogId = attrGroupEntity.getCatelogId();
        //先查询分类下所有的属性分组，再查出关联的属性排除
        List<AttrGroupEntity> attrGroupEntityList = this.list(new LambdaUpdateWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));

        List<Long> attrGroupIds = attrGroupEntityList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                in(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupIds));
        List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        //查出未被关联的属性
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        lambdaQueryWrapper.eq(AttrEntity::getCatelogId, catelogId).
                notIn(attrIds.size() > 0, AttrEntity::getAttrId, attrIds).
                eq(AttrEntity::getAttrType, ProductConstants.AttrType.ATTR_TYPE_BASE.getCode()).
                and(StringUtils.isNotBlank(key), wrapper -> wrapper.eq(AttrEntity::getAttrId, key).or().eq(AttrEntity::getAttrName, key));
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), lambdaQueryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveAttrRelated(List<AttrGroupRelationVo> attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList = attrGroupRelationVos.stream().map(vos -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(vos.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(vos.getAttrGroupId());
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntityList);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaUpdateWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            List<AttrEntity> attrRelation = this.getAttrRelation(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrRelation);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }
}